<template>
	<!-- 个人信息页面: -->
	<view class="personInfo">
		<!-- 包括多个修改信息的选项: -->
		<u-cell-group title-bg-color="rgb(243, 244, 246)">
			<u-cell :titleStyle="{fontWeight: 500}" :title="item.title" v-for="(item, index) in userInfo" :key="index"
				isLink :icon="item.iconUrl" @click="goRepair('././repairInfo/repair',1,userInfo[index])">
			</u-cell>
		</u-cell-group>
	</view>
</template>

<script>
	import {
		goDetail
	} from '@/utils/utils.js';
	import {
		myToken
	} from '@/utils/utils.js';
	export default {
		data() {
			return {
				show: false,
				// 用户id：
				userId: '',
				value: '',
				// 本机用户对象:
				userInfo: [{
						title: '用户性别',
						iconUrl: 'https://s2.loli.net/2022/05/02/vFrclnokD3fdIXZ.png',
						nickName: 'sex',
						value: 'man'
					},
					{
						title: '手机号码',
						iconUrl: 'https://s2.loli.net/2022/05/02/ra8bueIvnBF1dDz.png',
						nickName: 'phone',
						value: '15297765394'
					},
					{
						title: 'QQ',
						iconUrl: 'https://s2.loli.net/2022/05/02/BP9nzVN8sGDowgH.png',
						nickName: 'QQ',
						value: '2174201308'
					},
					{
						title: '邮箱',
						iconUrl: 'https://s2.loli.net/2022/05/02/Wd7VEl1DL2wT6ng.png',
						nickName: 'email',
						value: '2174201308@qq.com'
					},
					{
						title: '职业',
						iconUrl: 'https://s2.loli.net/2022/05/02/dnawvzXE2BPky5G.png',
						nickName: 'professional',
						// selections: [],
						value: 'student'
					}, {
						title: '家庭住址',
						iconUrl: 'https://s2.loli.net/2022/05/02/bmtZBiO53Pzjpd8.png',
						nickName: 'address',
						value: 'China'
					}
				],
				// 职业选项:
				// professionalSelection: [],
			}
		},
		// 模拟缓存登录的用户信息:
		onLoad() {
			// this.userStorage();
			this.getAllUserInfo();
		},
		methods: {
			goRepair(path, num, params) {
				goDetail(path, num, params);
			},
			// 获取当前用户信息:
			getAllUserInfo() {
				let allUserInfo = new Promise((resolve, reject) => {
					uni.request({
						url: '/user/get/information',
						method: 'GET',
						success: (res) => {
							resolve(res);
						},
						fail: (err) => {
							reject(err)
						}
					})
				})
				allUserInfo.then(({
					data: res
				}) => {
					console.log(res)
					console.log('当前用户信息:', res)
					this.userInfo[1].value = res.data.phone;
					this.userInfo[2].value = res.data.qq;
					this.userInfo[3].value = res.data.email;
					this.userInfo[4].value = res.data.professional;
					this.userInfo[5].value = res.data.address;
					this.userId = res.id;
					for (let i = 0; i < this.userInfo.length; i++) {
						if (this.userInfo[i].nickName === 'address') {
							this.userInfo[i].value = res.data.address;
						} else if (this.userInfo[i].nickName === 'email') {
							this.userInfo[i].value = res.data.email;
						} else if (this.userInfo[i].nickName === 'phone') {
							this.userInfo[i].value = res.data.phone;
						} else if (this.userInfo[i].nickName === 'professional') {
							this.userInfo[i].value = res.data.professional;
							this.userInfo[i].selections=res.data.professionals;
						} else if (this.userInfo[i].nickName === 'qq') {
							this.userInfo[i].value = res.data.qq;
						}
					}
					// 获取职业选项数据:
					// this.professionalSelection=res.data.professionals
					// console.log('显示当前用户所有信息:', this.userInfo)
					// // console.log('显示当前用的的职业选项数据:',this.professionalSelection)
					// try {
					// 	uni.setStorageSync('userInfo', ...this.userInfo);
					// } catch (e) {
					// 	//TODO handle the exception
					// }
				}).catch(err => {
					console.log('获取用户信息失败:', err)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.personInfo {
		display: flex;
		flex-direction: column;
	}
</style>
